<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
                "http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
<head>
  <title>Description of demgmm3</title>
  <meta name="keywords" content="demgmm3">
  <meta name="description" content="DEMGMM3 Demonstrate density modelling with a Gaussian mixture model.">
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  <meta name="generator" content="m2html &copy; 2003 Guillaume Flandin">
  <meta name="robots" content="index, follow">
  <link type="text/css" rel="stylesheet" href="../../m2html.css">
</head>
<body>
<a name="_top"></a>
<div><a href="../../menu.html">Home</a> &gt;  <a href="#">ReBEL-0.2.7</a> &gt; <a href="#">netlab</a> &gt; demgmm3.m</div>

<!--<table width="100%"><tr><td align="left"><a href="../../menu.html"><img alt="<" border="0" src="../../left.png">&nbsp;Master index</a></td>
<td align="right"><a href="menu.html">Index for .\ReBEL-0.2.7\netlab&nbsp;<img alt=">" border="0" src="../../right.png"></a></td></tr></table>-->

<h1>demgmm3
</h1>

<h2><a name="_name"></a>PURPOSE <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2>
<div class="box"><strong>DEMGMM3 Demonstrate density modelling with a Gaussian mixture model.</strong></div>

<h2><a name="_synopsis"></a>SYNOPSIS <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2>
<div class="box"><strong>This is a script file. </strong></div>

<h2><a name="_description"></a>DESCRIPTION <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2>
<div class="fragment"><pre class="comment">DEMGMM3 Demonstrate density modelling with a Gaussian mixture model.

    Description
     The problem consists of modelling data generated by a mixture of
    three Gaussians in 2 dimensions with a mixture model using diagonal
    covariance matrices.  The priors are 0.3, 0.5 and 0.2; the centres
    are (2, 3.5), (0, 0) and (0,2); the covariances are all axis aligned
    (0.16, 0.64), (0.25, 1) and the identity matrix. The first figure
    contains a scatter plot of the data.

    A Gaussian mixture model with three components is trained using EM.
    The parameter vector is printed before training and after training.
    The user should press any key to continue at these points.  The
    parameter vector consists of priors (the column), and centres (given
    as (x, y) pairs as the next two columns).  The diagonal entries of
    the covariance matrices are printed separately.

    The second figure is a 3 dimensional view of the density function,
    while the third shows the axes of the 1-standard deviation circles
    for the three components of the mixture model.

    See also
    <a href="gmm.html" class="code" title="function mix = gmm(dim, ncentres, covar_type, ppca_dim)">GMM</a>, <a href="gmminit.html" class="code" title="function mix = gmminit(mix, x, options)">GMMINIT</a>, <a href="gmmem.html" class="code" title="function [mix, options, errlog] = gmmem(mix, x, options)">GMMEM</a>, <a href="gmmprob.html" class="code" title="function prob = gmmprob(mix, x)">GMMPROB</a>, <a href="gmmunpak.html" class="code" title="function mix = gmmunpak(mix, p)">GMMUNPAK</a></pre></div>

<!-- crossreference -->
<h2><a name="_cross"></a>CROSS-REFERENCE INFORMATION <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2>
This function calls:
<ul style="list-style-image:url(../../matlabicon.gif)">
<li><a href="gmm.html" class="code" title="function mix = gmm(dim, ncentres, covar_type, ppca_dim)">gmm</a>	GMM	Creates a Gaussian mixture model with specified architecture.</li><li><a href="gmmem.html" class="code" title="function [mix, options, errlog] = gmmem(mix, x, options)">gmmem</a>	GMMEM	EM algorithm for Gaussian mixture model.</li><li><a href="gmminit.html" class="code" title="function mix = gmminit(mix, x, options)">gmminit</a>	GMMINIT Initialises Gaussian mixture model from data</li><li><a href="gmmprob.html" class="code" title="function prob = gmmprob(mix, x)">gmmprob</a>	GMMPROB Computes the data probability for a Gaussian mixture model.</li></ul>
This function is called by:
<ul style="list-style-image:url(../../matlabicon.gif)">
<li><a href="demnlab.html" class="code" title="function demnlab(action);">demnlab</a>	DEMNLAB A front-end Graphical User Interface to the demos</li></ul>
<!-- crossreference -->


<h2><a name="_source"></a>SOURCE CODE <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2>
<div class="fragment"><pre>0001 <span class="comment">%DEMGMM3 Demonstrate density modelling with a Gaussian mixture model.</span>
0002 <span class="comment">%</span>
0003 <span class="comment">%    Description</span>
0004 <span class="comment">%     The problem consists of modelling data generated by a mixture of</span>
0005 <span class="comment">%    three Gaussians in 2 dimensions with a mixture model using diagonal</span>
0006 <span class="comment">%    covariance matrices.  The priors are 0.3, 0.5 and 0.2; the centres</span>
0007 <span class="comment">%    are (2, 3.5), (0, 0) and (0,2); the covariances are all axis aligned</span>
0008 <span class="comment">%    (0.16, 0.64), (0.25, 1) and the identity matrix. The first figure</span>
0009 <span class="comment">%    contains a scatter plot of the data.</span>
0010 <span class="comment">%</span>
0011 <span class="comment">%    A Gaussian mixture model with three components is trained using EM.</span>
0012 <span class="comment">%    The parameter vector is printed before training and after training.</span>
0013 <span class="comment">%    The user should press any key to continue at these points.  The</span>
0014 <span class="comment">%    parameter vector consists of priors (the column), and centres (given</span>
0015 <span class="comment">%    as (x, y) pairs as the next two columns).  The diagonal entries of</span>
0016 <span class="comment">%    the covariance matrices are printed separately.</span>
0017 <span class="comment">%</span>
0018 <span class="comment">%    The second figure is a 3 dimensional view of the density function,</span>
0019 <span class="comment">%    while the third shows the axes of the 1-standard deviation circles</span>
0020 <span class="comment">%    for the three components of the mixture model.</span>
0021 <span class="comment">%</span>
0022 <span class="comment">%    See also</span>
0023 <span class="comment">%    GMM, GMMINIT, GMMEM, GMMPROB, GMMUNPAK</span>
0024 <span class="comment">%</span>
0025 
0026 <span class="comment">%    Copyright (c) Ian T Nabney (1996-2001)</span>
0027 
0028 <span class="comment">% Generate the data</span>
0029 ndata = 500;
0030 
0031 <span class="comment">% Fix the seeds for reproducible results</span>
0032 randn(<span class="string">'state'</span>, 42);
0033 rand(<span class="string">'state'</span>, 42);
0034 data = randn(ndata, 2);
0035 prior = [0.3 0.5 0.2];
0036 <span class="comment">% Mixture model swaps clusters 1 and 3</span>
0037 datap = [0.2 0.5 0.3];
0038 datac = [0 2; 0 0; 2 3.5];
0039 datacov = [1 1;1 0.25; 0.4*0.4 0.8*0.8];
0040 data1 = data(1:prior(1)*ndata,:);
0041 data2 = data(prior(1)*ndata+1:(prior(2)+prior(1))*ndata, :);
0042 data3 = data((prior(1)+prior(2))*ndata +1:ndata, :);
0043 
0044 <span class="comment">% First cluster has axis aligned variance and centre (2, 3.5)</span>
0045 data1(:, 1) = data1(:, 1)*0.4 + 2.0;
0046 data1(:, 2) = data1(:, 2)*0.8 + 3.5;
0047 
0048 <span class="comment">% Second cluster has axis aligned variance and centre (0, 0)</span>
0049 data2(:,2) = data2(:, 2)*0.5;
0050 
0051 <span class="comment">% Third cluster is at (0,2) with identity matrix for covariance</span>
0052 data3 = data3 + repmat([0 2], prior(3)*ndata, 1);
0053 
0054 <span class="comment">% Put the dataset together again</span>
0055 data = [data1; data2; data3];
0056 
0057 clc
0058 disp(<span class="string">'This demonstration illustrates the use of a Gaussian mixture model'</span>)
0059 disp(<span class="string">'with diagonal covariance matrices to approximate the unconditional'</span>)
0060 disp(<span class="string">'probability density of data in a two-dimensional space.'</span>)
0061 disp(<span class="string">'We begin by generating the data from a mixture of three Gaussians'</span>)
0062 disp(<span class="string">'with axis aligned covariance structure and plotting it.'</span>)
0063 disp(<span class="string">' '</span>)
0064 disp(<span class="string">'The first cluster has centre (0, 2).'</span>)
0065 disp(<span class="string">'The second cluster has centre (0, 0).'</span>)
0066 disp(<span class="string">'The third cluster has centre (2, 3.5).'</span>)
0067 disp(<span class="string">' '</span>)
0068 disp(<span class="string">'Press any key to continue'</span>)
0069 pause
0070 
0071 fh1 = figure;
0072 plot(data(:, 1), data(:, 2), <span class="string">'o'</span>)
0073 set(gca, <span class="string">'Box'</span>, <span class="string">'on'</span>)
0074 
0075 <span class="comment">% Set up mixture model</span>
0076 ncentres = 3;
0077 input_dim = 2;
0078 mix = <a href="gmm.html" class="code" title="function mix = gmm(dim, ncentres, covar_type, ppca_dim)">gmm</a>(input_dim, ncentres, <span class="string">'diag'</span>);
0079 
0080 options = foptions;
0081 options(14) = 5;    <span class="comment">% Just use 5 iterations of k-means in initialisation</span>
0082 <span class="comment">% Initialise the model parameters from the data</span>
0083 mix = <a href="gmminit.html" class="code" title="function mix = gmminit(mix, x, options)">gmminit</a>(mix, data, options);
0084 
0085 <span class="comment">% Print out model</span>
0086 disp(<span class="string">'The mixture model has three components and diagonal covariance'</span>)
0087 disp(<span class="string">'matrices.  The model parameters after initialisation using the'</span>)
0088 disp(<span class="string">'k-means algorithm are as follows'</span>)
0089 disp(<span class="string">'    Priors        Centres'</span>)
0090 disp([mix.priors' mix.centres])
0091 disp(<span class="string">'Covariance diagonals are'</span>)
0092 disp(mix.covars)
0093 disp(<span class="string">'Press any key to continue.'</span>)
0094 pause
0095 
0096 <span class="comment">% Set up vector of options for EM trainer</span>
0097 options = zeros(1, 18);
0098 options(1)  = 1;        <span class="comment">% Prints out error values.</span>
0099 options(14) = 20;        <span class="comment">% Number of iterations.</span>
0100 
0101 disp(<span class="string">'We now train the model using the EM algorithm for 20 iterations.'</span>)
0102 disp(<span class="string">' '</span>)
0103 disp(<span class="string">'Press any key to continue.'</span>)
0104 pause
0105 
0106 [mix, options, errlog] = <a href="gmmem.html" class="code" title="function [mix, options, errlog] = gmmem(mix, x, options)">gmmem</a>(mix, data, options);
0107 
0108 <span class="comment">% Print out model</span>
0109 disp(<span class="string">' '</span>)
0110 disp(<span class="string">'The trained model has priors and centres:'</span>)
0111 disp(<span class="string">'    Priors        Centres'</span>)
0112 disp([mix.priors' mix.centres])
0113 disp(<span class="string">'The data generator has priors and centres'</span>)
0114 disp(<span class="string">'    Priors        Centres'</span>)
0115 disp([datap' datac])
0116 disp(<span class="string">'Model covariance diagonals are'</span>)
0117 disp(mix.covars)
0118 disp(<span class="string">'Data generator covariance diagonals are'</span>)
0119 disp(datacov)
0120 disp(<span class="string">'Note the close correspondence between these parameters and those'</span>)
0121 disp(<span class="string">'of the distribution used to generate the data.'</span>)
0122 disp(<span class="string">' '</span>)
0123 disp(<span class="string">'Press any key to continue.'</span>)
0124 pause
0125 
0126 clc
0127 disp(<span class="string">'We now plot the density given by the mixture model as a surface plot.'</span>)
0128 disp(<span class="string">' '</span>)
0129 disp(<span class="string">'Press any key to continue.'</span>)
0130 pause
0131 
0132 <span class="comment">% Plot the result</span>
0133 x = -4.0:0.2:5.0;
0134 y = -4.0:0.2:5.0;
0135 [X, Y] = meshgrid(x,y);
0136 X = X(:);
0137 Y = Y(:);
0138 grid = [X Y];
0139 Z = <a href="gmmprob.html" class="code" title="function prob = gmmprob(mix, x)">gmmprob</a>(mix, grid);
0140 Z = reshape(Z, length(x), length(y));
0141 c = mesh(x, y, Z);
0142 hold on
0143 title(<span class="string">'Surface plot of probability density'</span>)
0144 hold off
0145 drawnow
0146 
0147 clc
0148 disp(<span class="string">'The final plot shows the centres and widths, given by one standard'</span>)
0149 disp(<span class="string">'deviation, of the three components of the mixture model.  The axes'</span>)
0150 disp(<span class="string">'of the ellipses of constant density are shown.'</span>)
0151 disp(<span class="string">' '</span>)
0152 disp(<span class="string">'Press any key to continue.'</span>)
0153 pause
0154 
0155 <span class="comment">% Try to calculate a sensible position for the second figure, below the first</span>
0156 fig1_pos = get(fh1, <span class="string">'Position'</span>);
0157 fig2_pos = fig1_pos;
0158 fig2_pos(2) = fig2_pos(2) - fig1_pos(4);
0159 fh2 = figure(<span class="string">'Position'</span>, fig2_pos);
0160 
0161 h = plot(data(:, 1), data(:, 2), <span class="string">'bo'</span>);
0162 hold on
0163 axis(<span class="string">'equal'</span>);
0164 title(<span class="string">'Plot of data and covariances'</span>)
0165 <span class="keyword">for</span> i = 1:ncentres
0166   v = [1 0];
0167   <span class="keyword">for</span> j = 1:2
0168     start=mix.centres(i,:)-sqrt(mix.covars(i,:).*v);
0169     endpt=mix.centres(i,:)+sqrt(mix.covars(i,:).*v);
0170     linex = [start(1) endpt(1)];
0171     liney = [start(2) endpt(2)];
0172     line(linex, liney, <span class="string">'Color'</span>, <span class="string">'k'</span>, <span class="string">'LineWidth'</span>, 3)
0173     v = [0 1];
0174   <span class="keyword">end</span>
0175   <span class="comment">% Plot ellipses of one standard deviation</span>
0176   theta = 0:0.02:2*pi;
0177   x = sqrt(mix.covars(i,1))*cos(theta) + mix.centres(i,1);
0178   y = sqrt(mix.covars(i,2))*sin(theta) + mix.centres(i,2);
0179   plot(x, y, <span class="string">'r-'</span>);
0180 <span class="keyword">end</span>
0181 hold off
0182 
0183 disp(<span class="string">'Note how the data cluster positions and widths are captured by'</span>)
0184 disp(<span class="string">'the mixture model.'</span>)
0185 disp(<span class="string">' '</span>)
0186 disp(<span class="string">'Press any key to end.'</span>)
0187 pause
0188 
0189 close(fh1);
0190 close(fh2);
0191 clear all;
0192</pre></div>
<hr><address>Generated on Tue 26-Sep-2006 10:36:21 by <strong><a href="http://www.artefact.tk/software/matlab/m2html/">m2html</a></strong> &copy; 2003</address>
</body>
</html>